package com.atguigu.yygh.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/*==========================================
      时 间:   2022-04-16
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
public class DictEvoListener extends AnalysisEventListener<DictEeVo> {

    private DictMapper dictMapper;


    public DictEvoListener(DictMapper dictMapper){
        this.dictMapper=dictMapper;
    }

    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo,dict);
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<Dict>();
        queryWrapper.eq("id",dictEeVo.getId());
        Integer count = this.dictMapper.selectCount(queryWrapper);
        if(count > 0){
            this.dictMapper.updateById(dict);
        }else{
            this.dictMapper.insert(dict);
        }


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
       // this.dictMapper.batchInsert(list);
    }
}
